import asyncio
import aioredis


async def execute(host, password, task_id="", port=6379):
    print(f"{task_id}开始执行", host)
    # 发生等待，可以切换
    redis = await aioredis.Redis(host=host, port=port, password=password)
    # 发生等待，可以切换
    await redis.hset("car", "key1", 1)
    # 发生等待，可以切换
    await redis.hmset("hash_key", mapping={
        "name": "John Doe",
        "age": 30,
        "email": "john.doe@example.com"
    })
    # 发生等待，可以切换
    result = await redis.hgetall("car")
    print(result)
    # 发生等待，可以切换
    result = await redis.hgetall("hash_key")
    print(result)
    # 发生等待，可以切换
    await redis.close()
    print(f"{task_id}结束", host)



# # 单个任务
# asyncio.run(execute("127.0.0.1", "1"))

# 鑫任务并发
tasks = [
    execute("127.0.0.1", "1", "1号"),
    execute("127.0.0.1", "1", "2号")
]
asyncio.run(asyncio.wait(tasks))